package com.pp.manage.controller;

import com.pp.manage.pojo.User;
import com.pp.manage.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理接口
 * @author yangfan
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 新增用户
     * @param user 新增用户信息
     * @return org.springframework.http.ResponseEntity
     */
    @PostMapping
    public ResponseEntity<Void> insertUser(@RequestBody User user) {
        if (this.userService.insertUser(user)) {
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 临时删除用户
     * @param userId 用户ID
     * @return org.springframework.http.ResponseEntity
     */
    @DeleteMapping("/delete/{userId}")
    public ResponseEntity<Void> deleteUserTemporary(@PathVariable Integer userId) {
        if (this.userService.deleteUserTemporary(userId)) {
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 永久删除用户
     * @param userId 用户ID
     * @return org.springframework.http.ResponseEntity
     */
    @DeleteMapping("/{userId}")
    public ResponseEntity<Void> deleteUser(@PathVariable Integer userId) {
        if (this.userService.deleteUser(userId)) {
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 更新用户信息
     * @param user 要更新的用户信息
     * @return org.springframework.http.ResponseEntity
     */
    @PutMapping
    public ResponseEntity<Void> updateUser(@RequestBody User user) {
        if (this.userService.updateUser(user)) {
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 按招用户ID值查找用户并返回用户信息
     * @param userId 用户ID
     * @return org.springframework.http.ResponseEntity
     */
    @GetMapping("/{userId}")
    public ResponseEntity<User> findUserById(@PathVariable Integer userId) {
        User user = this.userService.findUserById(userId);
        if (user != null) {
            return ResponseEntity.ok(user);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查找所有用户（不包括状态为-1的用户）
     * @return org.springframework.http.ResponseEntity
     */
    @GetMapping
    public ResponseEntity<List<User>> findAllUser() {
        List<User> userList = this.userService.findAllUser();
        if (!CollectionUtils.isEmpty(userList)) {
            return ResponseEntity.ok(userList);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
